package com.offcn.config;

import com.offcn.handler.AuthenticationEntryPointHandler;
import com.offcn.handler.SecurityAccessDeniedHandler;
import com.offcn.handler.SecurityAuthenticationFailureHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    @Autowired
    private UserDetailsService userDetailsService;
    @Autowired
    private AuthenticationEntryPointHandler authenticationEntryPointHandler;
    @Autowired
    private SecurityAccessDeniedHandler securityAccessDeniedHandler;
    @Autowired
    private SecurityAuthenticationFailureHandler securityAuthenticationFailureHandler;


    @Bean
    protected PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
      auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder());
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        //使用iframe显示资源
        http.csrf().disable().headers().frameOptions().sameOrigin();
        //表单登录
        http .formLogin()
                //登录页面
                .loginPage("/index.html")
                //登录访问路径，与页面表单提交路径一致
                .loginProcessingUrl("/login")
                //登录成功后访问路径
                .defaultSuccessUrl("/pages/main.html").permitAll()
                //登录失败操作
                .failureHandler(securityAuthenticationFailureHandler)
                .and()
                //认证配置
                .authorizeRequests()
                .antMatchers("/index.html", "/login").permitAll()
                //配置静态页面可以访问
                .antMatchers("/css/**","/img/**","/js/**","/loginstyle/**","/plugins/**").permitAll()
                //任何请求
                .anyRequest()
                //都需要身份验证
                .authenticated();
        http.exceptionHandling().authenticationEntryPoint(authenticationEntryPointHandler).accessDeniedHandler(securityAccessDeniedHandler);
        //配置退出
        http.logout()
                //退出路径
                .logoutUrl("/logout")
                //退出后跳转页面
                .logoutSuccessUrl("/index.html");
    }
}
